package org.linlinjava.litemall.wx.service;

import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.PiccNurse;
import org.linlinjava.litemall.db.domain.PiccPatient;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.PiccNurseService;
import org.linlinjava.litemall.db.service.PiccPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WxPiccPatientService {
    @Autowired
    PiccPatientService piccPatientService;

    @Autowired
    PiccNurseService piccNurseService;

    @Autowired
    LitemallUserService litemallUserService;

    public PiccPatient add(PiccPatient info){
        if (info.getId()==0) {
            //根据工号查询护士id，然后设置
            String jobNum = info.getJobnum();
            PiccNurse nurse = piccNurseService.getDetailByJobNum(jobNum);
            info.setNurseId(nurse.getId());
            piccPatientService.add(info);
            litemallUserService.updateRole(info.getUserId(), "patient");

            LitemallUser user = litemallUserService.findById(info.getUserId());
            user.setRole("patient");

            return info;
        }else if (info.getId()>0){
            PiccPatient patient = piccPatientService.getDetailByUserId(info.getUserId());
            if (patient == null) {
                return null;
            }

            info.setNurseId(patient.getNurseId());
            info.setStatus(patient.getStatus());
            //info.setJobnum(patient.getJobnum());
            piccPatientService.update(info);
            litemallUserService.updateRole(info.getUserId(), "patient");
            return info;
        }else
            return new PiccPatient();
    }

    public Integer update(PiccPatient patient) {
        if (patient == null) {
            return 402;
        }

        PiccPatient piccPatient = piccPatientService.getDetailById(patient.getId());
        if (piccPatient == null) {
            return 402;
        }

        patient.setUserId(piccPatient.getUserId());
        if (!patient.equals(piccPatient)) {
            piccPatientService.update(patient);
        }
        return 0;
    }
}
